package manager;

/**
 * @author huangjm.2012, huiwensong.2012, sheryl.tay.2012, mohameds.2012
 *
 */

import java.util.ArrayList;

import entity.Course;
import exception.ValidationError;

public class CourseMgr {

	DatabaseMgr dbMgr;

	public CourseMgr() {
		dbMgr = new DatabaseMgr();
	}

	public void addCourse(Course course) {

		String sql = "insert into course "
				+ " (courseCode, school, title, description, examDate, examStartTime, examEndTime) "
				+ " values (?, ?, ?, ?, ?, ?, ?) ";

		dbMgr.openAndUpdateAndClose(sql, course.getCourseCode(),
				course.getSchool(), course.getTitle(), course.getDescription(),
				course.getExamDate(), course.getExamStartTime(),
				course.getExamEndTime());
	}

	public Course retrieveCourseForCourseCode(String courseCode)
			throws ValidationError {

		String sql = "select * from course where courseCode=? order by courseCode";

		Course course = RetrieveCourseUtil.retrieveCourseForQuery(dbMgr, sql,
				courseCode);

		if (course == null) {
			throw new ValidationError("invalid course");
		}
		return course;
	}

	public ArrayList<Course> retrieveAllCourses() {
		return RetrieveCourseUtil.retrieveMultipleCoursesForQuery(dbMgr,
				"select * from course order by courseCode");
	}

	public ArrayList<Course> retrieveCoursesOfferedBySchool(String school) {

		return RetrieveCourseUtil.retrieveMultipleCoursesForQuery(dbMgr,
				"select * from course where school=? order by courseCode",
				school);
	}

	public void deleteAllCourses() {
		dbMgr.openAndUpdateAndClose("delete from course");
	}

}
